
#include <QRadioButton>

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "utils.h"

class MainWindowPrivate
{
public:
    MainWindowPrivate(MainWindow *parent = nullptr) : q_ptr(parent) {}

private:
    MainWindow * const q_ptr;
    Q_DECLARE_PUBLIC(MainWindow)
};

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), d_ptr(new MainWindowPrivate(this))
{
    ui->setupUi(this);
    setWindowIcon(QIcon(QLatin1String(":/icons/kylin-compile-env-builder.svg")));

    ui->workmodes->setEnabled(false);

    MiscUtils::LoadLanguage(qApp->applicationName());
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::OnPluginLoaded(const QString &modename, QWidget *widget, bool is_default)
{
    qDebug().noquote() << "UI Attach Plugin:" << widget << "ModeName:" << "[" << modename << "]" << "Default:" << is_default;

    QRadioButton *mode_button = new QRadioButton(modename);
    connect(mode_button, &QRadioButton::toggled, this, &MainWindow::OnPluginToggled);
    mode_button->setChecked(is_default);
    ui->hLayout_workmode->addWidget(mode_button);

    ui->stackedWidget->insertWidget(ui->stackedWidget->count(), widget);
    if (is_default) {
        ui->stackedWidget->setCurrentWidget(widget);
    } else {
        ui->stackedWidget->setCurrentIndex(0);
    }

    ui->workmodes->setEnabled(ui->hLayout_workmode->count() > 1);
}

void MainWindow::OnPluginToggled(bool checked)
{
    Q_UNUSED(checked)
    QRadioButton *button = dynamic_cast<QRadioButton*>(sender());
    qDebug().noquote() << "Plugin Toggled" << "[" << button->text() << "]";

    emit CurrentPluginChanged(button->text());
}
